package com.example.expandablelistview.listview;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.expandablelistview.R;

public class ListIconText extends Activity {
	private ArrayList<MyItem> arItems;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listicontext);

		arItems = new ArrayList<ListIconText.MyItem>();
		MyItem mi;
		mi = new MyItem(R.drawable.ic_launcher, "삼성 노트북");
		arItems.add(mi);
		mi = new MyItem(R.drawable.ic_launcher, "LG 세탁기");
		arItems.add(mi);
		mi = new MyItem(R.drawable.ic_launcher, "대우 마티즈");
		arItems.add(mi);

		MyListAdapter myAdapter = new MyListAdapter(this, arItems);
		ListView myList = (ListView) findViewById(R.id.list);
		myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				Toast.makeText(getApplicationContext(), arItems.get(position).mName, Toast.LENGTH_LONG).show();
			}
		});
		myList.setAdapter(myAdapter);
	}

	class MyItem {
		int mIcon;
		String mName;

		public MyItem(int mIcon, String mName) {
			this.mIcon = mIcon;
			this.mName = mName;
		}
	}// end of class MyItem

	class MyListAdapter extends BaseAdapter {
		private LayoutInflater inflater;
		private ArrayList<MyItem> arItems;


		public MyListAdapter(Context context, ArrayList<MyItem> aarItems) {
			this.arItems = aarItems;
			this.inflater = LayoutInflater.from(context);
		}

		public int getCount() {
			return arItems.size();
		}

		public Object getItem(int position) {
			return arItems.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		class ViewHolder{
			ImageView imgView;
			TextView textView;
			Button btn;
		}
		
		public View getView(int position, View convertView, ViewGroup parent) {
			final int pos = position;
			View itemLayout = convertView;
			ViewHolder viewHolder = null;
			
			if (itemLayout == null) {
				itemLayout = inflater.inflate(R.layout.icontext, parent, false);
				viewHolder = new ViewHolder();
				viewHolder.imgView = (ImageView) itemLayout.findViewById(R.id.img);
				viewHolder.textView = (TextView) itemLayout.findViewById(R.id.text);
				viewHolder.btn = (Button) itemLayout.findViewById(R.id.btn);
				itemLayout.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) itemLayout.getTag();
			}
			viewHolder.imgView.setImageResource(arItems.get(position).mIcon);
			viewHolder.textView.setText(arItems.get(position).mName);
			viewHolder.btn.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					String text = arItems.get(pos).mName + "를 주문합니다.";
					Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
				}
			});
			return itemLayout;
		}
		
	}// end of class MyListAdapter
}// end of class ListIconText

